package WizardInstall;
/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic

ISBN: 1932394273

Publisher: Manning
 */


import java.io.File;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;

import Server.SendInfo;
import Server.ServerProtocol;
import Server.StartWeb;
import XML.ConfigHelper;



/**
 * Install Wizard manage all windows
 * @author Guy & Liel
 *
 */
class ProjectWizard extends Wizard {


	public ProjectWizard() {
		super();
	}
	/**
	 * return true if can finish the install otherwise false
	 */
	public boolean canFinish()
	{
		if(getContainer().getCurrentPage()==getSummaryPage())
		{
			return true;
		}
		return false;

	}
	/**
	 * add all the pages that can show in installation
	 */
	public void addPages() 
	{
		addPage(new WelcomePage());
		addPage(new LoginDBPage());
		addPage(new ChooseDirectoryPage());
		addPage(new SummaryPage());
	}

	/**
	 * start run the server by thread
	 */
	public boolean performFinish() 
	{

		String mainServerFolder=getChoosePage().getDirectory();
		String dbUserName=getLoginDBPage().getUserName();
		String dbPassword=getLoginDBPage().getPassword();
		
		ServerDetails serverDetails=ServerDetails.getServerDetailInstance();
		serverDetails.setUserNameDB(dbUserName);
		serverDetails.setPasswordDB(dbPassword);
		serverDetails.setServerFilePath(mainServerFolder);
		try
		{
			ConfigHelper.writeXML(dbUserName, dbPassword, mainServerFolder);
			StartWeb sw=new StartWeb();
			Thread t = new Thread(sw);
			t.start();
			
			ServerProtocol server = ServerProtocol.getInstance();
			SendInfo sendInfo = SendInfo.getInstance();
			//run both sending and receiving manager
			Thread t1 = new Thread(server);
			Thread t2 = new Thread(sendInfo);
			t1.start();
			t2.start();
			return true;
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;


		
	}


	private ChooseDirectoryPage getChoosePage() {
		return (ChooseDirectoryPage) getPage(ChooseDirectoryPage.PAGE_NAME);
	}
	private LoginDBPage getLoginDBPage()
	{
		return (LoginDBPage)getPage(LoginDBPage.PAGE_NAME);
	}
	private SummaryPage getSummaryPage() {
		return (SummaryPage) getPage(SummaryPage.PAGE_NAME);
	}
	

	public boolean performCancel() {
		System.out.println("Perform Cancel called");
		return true;
	}
	/**
	 * Take the next page in the installation
	 */
	public IWizardPage getNextPage(IWizardPage page) 
	{
		//Asking if User Exist
		if (page instanceof WelcomePage) 
		{
		
			LoginDBPage loginDBPage=(LoginDBPage) getPage(LoginDBPage.PAGE_NAME);
			return loginDBPage;

		}
		IWizardPage nextPage = super.getNextPage(page);
		//Check User Exist
		if(page instanceof ChooseDirectoryPage)
		{
			String strFolder=getChoosePage().getDirectory().toString();
			if(strFolder!="")
			{
				File file=new File(strFolder.toString());
				boolean exists = file.exists();
				if (exists) 
				{
					String strSummry="Server installed";
					SummaryPage summaryPage=(SummaryPage) getPage(SummaryPage.PAGE_NAME);
					summaryPage.updateText(strSummry);
					return summaryPage;
				}
			}
			ChooseDirectoryPage chooseDirectoryPage=(ChooseDirectoryPage) getPage(ChooseDirectoryPage.PAGE_NAME);
			chooseDirectoryPage.updateTextError("Folder dosen't Exist,please select folder");
			return chooseDirectoryPage;
		}
		if(page instanceof LoginDBPage)
		{
			String userName=getLoginDBPage().getUserName();
			String password=getLoginDBPage().getPassword();

			if(userName.isEmpty() || password.isEmpty())
			{
				System.out.println("OK");
				LoginDBPage loginDBPage=(LoginDBPage)getPage(LoginDBPage.PAGE_NAME);
				loginDBPage.setError("You have to filled all the fileds");
				return loginDBPage;
			}
			else
			{
				ChooseDirectoryPage chooseDirectoryPage=(ChooseDirectoryPage) getPage(ChooseDirectoryPage.PAGE_NAME);
				return chooseDirectoryPage;
			}
			/*SummaryPage summaryPage=(SummaryPage) getPage(SummaryPage.PAGE_NAME);
			String str="Thank you for installing Clever DropBox \n\n";
			str+="Your details are:\n\n";
			if(getLoginPage().getUserName().isEmpty())
			{
				str+="User Name:"+getRegisterPage().getUserName().toString()+"\n";
			}
			else
			{
				str+="User Name:"+getLoginPage().getUserName().toString()+"\n";
			}
			str+="Password:*******\n";			
			summaryPage.updateText(str);

			return summaryPage;*/
		}
		return nextPage;
	}
}